package com.cincc.cinccimdemo.ui.conversation.sharefile;

import android.content.Context;
import android.content.Intent;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.AttrConstants;
import com.cincc.cinccimdemo.common.FileUtils;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.ui.me.favorite.FavoriteListActivity;
import com.cincc.im.callback.ImCallback;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.ImResponse;

public class GroupShareFileActivity extends IMBaseActivity {

    private static final String TAG = "GroupShareFileActivity";
    public static final int FILE_REQUEST_CODE = 10005;

    @Override
    protected int contentLayout() {
        return R.layout.activity_group_share_file;
    }

    RecyclerView recyclerView;
    String groupId = "";

    @Override
    protected void bindViews() {
        super.bindViews();
        recyclerView = findViewById(R.id.recyclerView);
    }

    GroupShareFileViewModel groupShareFileViewModel;
    GroupShareFileAdapter groupShareFileAdapter;

    @Override
    protected void afterViews() {
        super.afterViews();
        groupId = getIntent().getStringExtra(AttrConstants.GROUP_ID);
        groupShareFileAdapter = new GroupShareFileAdapter(this);
        recyclerView.setAdapter(groupShareFileAdapter);
        groupShareFileViewModel = new ViewModelProvider(this).get(GroupShareFileViewModel.class);
        groupShareFileViewModel.shareFileListLiveData(groupId).observe(this, groups -> {
            groupShareFileAdapter.setGroupShareFileList(groups);
        });
    }


    @Override
    protected int menu() {
        return R.menu.group_share_file;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.upload_group_share_file) {
            uploadFile();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void delGroupShareFile(String fileId) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("确定要删除吗？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", (dialog, which) -> {
            IMSipPhoneCtrl.getInstance().delGroupShareFile(groupId, fileId, imResponse -> {
                String result = imResponse.getCode() == 0 ? "删除成功" : imResponse.getMsg();
                runOnUiThread(() -> {
                    Toast.makeText(GroupShareFileActivity.this, result, Toast.LENGTH_SHORT).show();
                    groupShareFileViewModel.reloadConversationList(groupId);
                });
            });
        });
        builder.show();

    }

    private void uploadFile() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");//无类型限制
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, FILE_REQUEST_CODE);
    }

    public static void startShareFilePage(Context context, String groupId) {
        Intent i = new Intent(context, GroupShareFileActivity.class);
        i.putExtra(AttrConstants.GROUP_ID, groupId);
        context.startActivity(i);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == FILE_REQUEST_CODE && data != null) {
            //处理从文件管理器选择了一个文件的结果
            String path = FileUtils.getPath(this, data.getData());
            IMSipPhoneCtrl.getInstance().addGroupShareFile(groupId, path, "", imResponse -> {
                String result = imResponse.getCode() == 0 ? "上传成功" : imResponse.getMsg();
                runOnUiThread(() -> {
                    Toast.makeText(GroupShareFileActivity.this, result, Toast.LENGTH_SHORT).show();
                    groupShareFileViewModel.reloadConversationList(groupId);
                });
            });
        }
    }
}
